﻿package classes.store.storeclasses 
{   
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent; 
	import flash.utils.getDefinitionByName;
	
	import classes.GameSetting;
	import classes.store.StoreRockman;
	
	import com.mttang.event.JEvent;
	import com.mttang.event.CommonEvent;
	
	
	/** 
	 * 交易商品逻辑处理类
	 * @author rock
	 */
	public class TransactionsGoods extends Sprite
	{
		//Ab或ab表示alertBox简写 
		public var storeAlertBoxMc:MovieClip;//弹出框
		private var alertGoodsMc:MovieClip;//弹出框里面的物品图片内容
		private var maskBm:Bitmap;
		
		private var kpsAlertDelMc:MovieClip;//提示框： 删除物品没选中时
		private var kpsAlertBuyMc:MovieClip;//提示框： 购买物品时 收藏夹没有任何物品 
		private var shopListAlertMc:MovieClip;//提示框： 收藏夹满了
		private var bagFullAlertMc:MovieClip;//提示框： 背包满了
		
		private var _nBagLength:Number = 28;//背包存放物品的总数
		
		public function TransactionsGoods() 
		{}
		//处理要交易的物品
		public function process(kpsArr:Array=null):void
		{  
			if (kpsArr.length==0)
			{
				trace("购买没有任何物品！");   
				showKpsBuyAlert();
			}
			else
			{
				var bagArr:Array = StoreRockman._saveData.bag;
			    var arrNum:Number = _nBagLength - bagArr.length - kpsArr.length;
			    if (arrNum >=0&&kpsArr.length!=0)
			    { 
				    drawMask();
				
		            storeAlertBoxMc = new StoreAlertBox(); 
			        addChild(storeAlertBoxMc);
			        storeAlertBoxMc.x=320-storeAlertBoxMc.width/2;
			        storeAlertBoxMc.y=240-storeAlertBoxMc.height/2;
			        storeAlertBoxMc.okBtn.addEventListener(MouseEvent.CLICK, abOkBtn_CkHandler);
			        storeAlertBoxMc.cencelBtn.addEventListener(MouseEvent.CLICK, abCencelBtn_CkHandler);
				
				    //显示收藏夹中物品总价格
			        var _nAllPrice:Number=0;
			        for (var i:uint = 0; i < kpsArr.length; i++ )
			        {  
			        	_nAllPrice += parseInt(kpsArr[i].price);
			        }   
			        storeAlertBoxMc.allMoney.text = "" + _nAllPrice + ""; 
			    } 
			    else
			    {
			     	showBagFullAlert();
			    } 
			}
		}
		//确认购买
		private function abOkBtn_CkHandler(e:MouseEvent):void
		{
		    dispatchEvent(new JEvent(CommonEvent.GOODS_BUY_OK));	
		}
		//取消购买
		private function abCencelBtn_CkHandler(e:MouseEvent):void
		{
		    dispatchEvent(new JEvent(CommonEvent.GOODS_BUY_CENCEL));	
		}
	
//*******************************************************************
		//友情提示：点击删除收藏夹物品时 没有选中物品 
		public function showKpsDelAlert():void
		{
		    drawMask();
			
			kpsAlertDelMc = new KpsAlerDelBox();
			addChild(kpsAlertDelMc);
			kpsAlertDelMc.x=320-kpsAlertDelMc.width/2;
			kpsAlertDelMc.y=240-kpsAlertDelMc.height/2;
			kpsAlertDelMc.ok.addEventListener(MouseEvent.CLICK, delOk_CkHandler);
		}
		private function delOk_CkHandler(e:MouseEvent):void
		{
			this.removeChild(kpsAlertDelMc);//删除弹出
			this.removeChild(maskBm);//删除遮罩层
		}
		//友情提示：点击购买收藏夹物品时 没有收藏物品时 
		private function showKpsBuyAlert():void
		{
			drawMask();
			
			kpsAlertBuyMc = new KpsAlertBuyBox();
		    addChild(kpsAlertBuyMc);
			kpsAlertBuyMc.x=320-kpsAlertBuyMc.width/2;
			kpsAlertBuyMc.y=240-kpsAlertBuyMc.height/2;
			kpsAlertBuyMc.ok.addEventListener(MouseEvent.CLICK, buyOk_CkHandler);
		}
		private function buyOk_CkHandler(e:MouseEvent):void
		{
			this.removeChild(kpsAlertBuyMc);//删除弹出
			this.removeChild(maskBm);//删除遮罩层
		}
		//友情提示：显示收藏夹满了提示框
		public function showShopListAlert():void
		{ 
			drawMask();
			
			shopListAlertMc = new ShopListAlertBox();
		    addChild(shopListAlertMc);
			shopListAlertMc.x=320-shopListAlertMc.width/2;
			shopListAlertMc.y=240-shopListAlertMc.height/2;
			shopListAlertMc.okBtn.addEventListener(MouseEvent.CLICK, slOk_CkHandler);
		}
		private function slOk_CkHandler(e:MouseEvent):void
		{
			this.removeChild(shopListAlertMc);//删除弹出
			this.removeChild(maskBm);//删除遮罩层
		}
		//友情提示：显示背包满了提示框
		private function showBagFullAlert():void
		{ trace("显示背包满了弹出框showBagFullAlert")
			drawMask();
			
			bagFullAlertMc = new BagFullAlertBox();
		    addChild(bagFullAlertMc);
			bagFullAlertMc.x=320-bagFullAlertMc.width/2;
			bagFullAlertMc.y=240-bagFullAlertMc.height/2;
			bagFullAlertMc.okBtn.addEventListener(MouseEvent.CLICK, bfOk_CkHandler);
		}
		private function bfOk_CkHandler(e:MouseEvent):void
		{
			this.removeChild(bagFullAlertMc);//删除弹出
			this.removeChild(maskBm);//删除遮罩层
		}
//*******************************************************************

		//创建遮罩层
		private function drawMask():void
		{ 
			var maskMd:BitmapData = new BitmapData(640, 480,false, 0xCCCCCC);
			maskBm = new Bitmap(maskMd);
			maskBm.alpha = 0.5;
			addChildAt(maskBm,this.numChildren);
		}
		public function cleanData():void
		{
			this.removeChild(storeAlertBoxMc);//删除弹出
			this.removeChild(maskBm);//删除遮罩层 
		}
	}

}